package com.jhgsys.internal.common.controller;

import com.jhgsys.internal.common.entity.DeptTree;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.system.entity.Dept;
import com.jhgsys.internal.system.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Validated
@RestController
public class CommonBasicDeptController extends BaseController{

    @Autowired
    private IDeptService deptService;


    @GetMapping("common/dept/tree")
    public FebsResponse getDeptTree(Dept dept) throws FebsException {
        try {
            if(StringUtils.isBlank(dept.getTenantId())){
                dept.setTenantId(getCurrentTenantId());
            }
            List<DeptTree<Dept>> deptList = this.deptService.findDepts(dept);
            return new FebsResponse().success().data(deptList);
        } catch (Exception e) {
            String message = "获取部门树失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
